package com.leostadyn.stackfarm.timestatistics.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.adapter.MainProjectAdapter;
import com.leostadyn.stackfarm.timestatistics.model.bean.DetailProjectBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class ProjectFragment extends Fragment implements MainProjectAdapter.OnRecyclerItemClickListener, OnRefreshListener {

    SmartRefreshLayout smartRefreshLayout;
    private ArrayList<DetailProjectBean> arrayList;
    private MainProjectAdapter adapter;
    private ProjectCallBack projectCallBack;
    private RecyclerView recyclerView;
    TextView txt;
    public double total = 0;

    public static ProjectFragment newInstance(ArrayList<DetailProjectBean> arrayList) {
        ProjectFragment projectFragment = new ProjectFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("project", arrayList);
        projectFragment.setArguments(bundle);
        return projectFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_project, container, false);
        initView(view);
        return view;
    }

    void initView(View view) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            arrayList = bundle.getParcelableArrayList("project");
            recyclerView = view.findViewById(R.id.mylist_project);
            recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
            txt = view.findViewById(R.id.txt_total_working_hours);
            txt.setText(projectCallBack.setProjectTitle());
            smartRefreshLayout = view.findViewById(R.id.project_srl_area);
            smartRefreshLayout.setOnRefreshListener(this);
            smartRefreshLayout.setEnableLoadMore(false);
            if (arrayList != null) {
                if (arrayList.size() == 0) {
                    recyclerView.setVisibility(View.GONE);
                    view.findViewById(R.id.layout_no_data).setVisibility(View.VISIBLE);
                } else {
                    for (int i = 0; i < arrayList.size(); i++) {
                        total += arrayList.get(i).getProjectWorkingHours();
                    }
                    projectCallBack.setPersonnelList(arrayList);
                    adapter = new MainProjectAdapter(arrayList);
                    adapter.setRecyclerItemClickListener(this);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setVisibility(View.VISIBLE);
                    view.findViewById(R.id.layout_no_data).setVisibility(View.GONE);
                }
            }
            projectCallBack.setPersonnelWorkingHours(total);
        }
    }

    private SetProjectCallBack callBack;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        projectCallBack = (ProjectCallBack) getActivity();
        callBack = (SetProjectCallBack) getActivity();
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        projectCallBack.sentProjectMessage();
    }

    @Override
    public void onItemClick(int position, List<DetailProjectBean> dataBeanList) {
        callBack.mainProjectClick(position, dataBeanList);
    }

    public interface SetProjectCallBack {
        void mainProjectClick(int position, List<DetailProjectBean> dataBeanList);
    }

    public interface ProjectCallBack {
        void sentProjectMessage();

        void setPersonnelList(ArrayList<DetailProjectBean> arrayList);
        String setProjectTitle();
        void setPersonnelWorkingHours(double d);
    }
}
